package com.tuu.configBean;
import com.tuu.mapper.JwtUserMapper;
import com.tuu.pojo.Permission;
import com.tuu.pojo.RPVersion;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.HashSet;
@Configuration
@Slf4j
public class InitConfig {
    @Autowired
    private JwtUserMapper jwtUserMapper;
    @Autowired
    private RedisUtil redisUtil;
    /*
    @Bean
    public FilterRegistrationBean buildAFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        //数字小  优先执行
        filterRegistrationBean.setOrder(2);
        filterRegistrationBean.setFilter(new AFilter());
        filterRegistrationBean.setName("filter1");
        filterRegistrationBean.addUrlPatterns("/**");
        return filterRegistrationBean;
    }
    @Bean
    public FilterRegistrationBean buildBFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        //数字小  优先执行
        filterRegistrationBean.setOrder(4);
        filterRegistrationBean.setFilter(new AFilter());
        filterRegistrationBean.setName("filter1");
        filterRegistrationBean.addUrlPatterns("/**");
        return filterRegistrationBean;
    }
*/
    @Bean
    public void initProject() {
        log.info("进行初始化。。。");
        ArrayList<RPVersion> version = jwtUserMapper.findVersion();
        if (version != null) {
            version.forEach((v)->{
                log.info(v.getName()+"---"+v.getVersionNum());
                redisUtil.set(v.getName(),String.valueOf(v.getVersionNum()) );
            });
        }
        HashSet<Permission> permissions = jwtUserMapper.findAllPermissons();
        log.info("初始化。。。从数据库取所有权限AllAuths放到redis");
        redisUtil.set("AllAuths",permissions);
        log.info("完成初始化。。。");
    }
}